#include <iostream>
#include "sqlite3.h"
#include "SQLiteManager.h"
#include "third_party/eventbus/Event.h"
#include "third_party/eventbus/EventBus.h"
#include "typeinfo"

class MyEvent : public Event {
public:
    int data; // 假设事件包含一些数据

    MyEvent(int d) : data(d) {}

    ~MyEvent() {

    }

    MyEvent *clone() const override {
        return new MyEvent(*this); // 创建并返回当前对象的深拷贝
    }
};

class MyEventListener : public EventListener {
    void onEvent(const Event &event) override {
        std::cout << "called" << std::endl;
        if (&event != nullptr) { // 确保指向的对象不是空指针
            if (typeid(event) == typeid(MyEvent)) {
                // event 是 MyEvent 类型的实例
                std::cout << "event is a MyEvent" << std::endl;
            } else {
                // event 不是 MyEvent 类型的实例
                std::cout << "event is not a MyEvent" << std::endl;
            }
        }
    }
};

int main() {

    // 这一段要提前，因为可能 myEvent 已经销毁了，但是 MyEventListener::onEvent 才被执行
    EventBus &bus = EventBus::getInstance();
    MyEventListener listener;

    bus.subscribe(typeid(MyEvent), &listener);

    MyEvent myEvent(1);
    bus.post(myEvent);

    bus.unsubscribe(typeid(MyEvent), &listener);

    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;

    rc = sqlite3_open("test.db", &db);

    if (rc) {
        std::cerr << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
        return 0;
    } else {
        std::cout << "Opened database successfully" << std::endl;
    }

    // ... 使用 SQLite 的其他功能
    // 创建 users 表
    std::string sql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER)";
    rc = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &zErrMsg);

    if (rc != SQLITE_OK) {
        std::cerr << "SQL error: " << zErrMsg << std::endl;
        sqlite3_free(zErrMsg);
    } else {
        std::cout << "Table 'users' created successfully" << std::endl;
    }

    // 插入数据
    sql = "INSERT INTO users (name, age, time) VALUES ('Alice', 25, time())";
    rc = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &zErrMsg);

    if (rc != SQLITE_OK) {
        std::cerr << "SQL error: " << zErrMsg << std::endl;
        sqlite3_free(zErrMsg);
    } else {
        std::cout << "Data inserted successfully" << std::endl;
    }

    // 查询数据
    sql = "SELECT * FROM users";
    sqlite3_stmt *stmt;

    rc = sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);

    if (rc == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int id = sqlite3_column_int(stmt, 0);
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            int age = sqlite3_column_int(stmt, 2);
            int time = sqlite3_column_int(stmt, 3);

            std::cout << "User ID: " << id << ", Name: " << name << ", Age: " << age << ", Time: " << time << std::endl;
        }
    } else {
        std::cerr << "SQL error: " << sqlite3_errmsg(db) << std::endl;
    }

    sqlite3_finalize(stmt);

    // 更新数据
    sql = "UPDATE users SET age = 26 WHERE name = 'Alice'";
    rc = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &zErrMsg);

    if (rc != SQLITE_OK) {
        std::cerr << "SQL error: " << zErrMsg << std::endl;
        sqlite3_free(zErrMsg);
    } else {
        std::cout << "Data updated successfully" << std::endl;
    }

    sql = "UPDATE users SET time = 26 WHERE time = 1";
    rc = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &zErrMsg);

    if (rc != SQLITE_OK) {
        std::cerr << "SQL error: " << zErrMsg << std::endl;
        sqlite3_free(zErrMsg);
    } else {
        std::cout << "Data updated successfully" << std::endl;
    }

    sqlite3_close(db);

    SQLiteManager db_manager("example.db");
    // 使用异步方式执行SQL命令
    auto future = db_manager.executeAsync(
            "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, value TEXT, time INT);");
    // 在这里可以执行其他任务...
    auto future2 = db_manager.executeAsync("insert into test (value, time) values (\"xxx\", random()*10);");
    auto future3 = db_manager.prepareAsync("select * from test;");

    // 等待数据库操作完成
    future.wait();
    future2.wait();
    future3.wait();
    stmt = future3.get();
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        int id = sqlite3_column_int(stmt, 0);
        const unsigned char *name = sqlite3_column_text(stmt, 1);
        int time = sqlite3_column_int(stmt, 2);
        std::cout << "User ID: " << id << ", Value: " << name << ", Time: " << time << std::endl;
    }
    sqlite3_finalize(stmt);

    std::cout << "Database operation completed." << std::endl;


    return 0;
}
